package com.wechat.shopping.mall.account.controller;

import com.wechat.shopping.mall.account.domain.SaveWechatUserParam;
import com.wechat.shopping.mall.account.service.UserInfoService;
import com.wechat.shopping.mall.util.ResponseMessage;
import com.wechat.shopping.mall.util.ValidUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@RestController
@RequestMapping(value = "/users")
@Api(value = "用户相关操作")
public class UserInfoController {

    private static final Logger LOG = LoggerFactory.getLogger(UserInfoController.class);

    @Autowired
    private UserInfoService userInfoService;

    @RequestMapping(value = "/user/info",method = RequestMethod.POST)
    @ApiOperation(value = "保存用户",notes = "用户注册",httpMethod = "POST",response = ResponseMessage.class)
    public ResponseMessage saveUser (@Valid @RequestBody SaveWechatUserParam user, BindingResult error) {
        if (error.hasErrors()) {
            return ResponseMessage.error(ValidUtils.getErrorMessage(error));
        }
        return userInfoService.saveUser(user);
    }

    @RequestMapping(value = "/user/{userId}/info",method = RequestMethod.PUT)
    @ApiOperation(value = "编辑用户",notes = "编辑用户",httpMethod = "PUT",response = ResponseMessage.class)
    public ResponseMessage updateUser (@PathVariable Integer userId,@Valid @RequestBody SaveWechatUserParam user, BindingResult error) {
        if (error.hasErrors()) {
            return ResponseMessage.error(ValidUtils.getErrorMessage(error));
        }
        return 1 == userInfoService.updateUser(user,userId)? ResponseMessage.ok("修改资料成功"):ResponseMessage.error("修改资料失败");
    }

    @RequestMapping(value = "/user/{userId}/info",method = RequestMethod.DELETE)
    @ApiOperation(value = "编辑用户",notes = "编辑用户",httpMethod = "DELETE",response = ResponseMessage.class)
    public ResponseMessage deleteUser (@PathVariable Integer userId) {
        return 1 == userInfoService.deleteUser(userId)? ResponseMessage.ok("注销账号成功"):ResponseMessage.error("注销账号失败");
    }


}
